import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'dart:math';

class DateUtil {
  static String getDuration(int seconds) {
    int hour = 0;
    int minute = 0;
    int second = 0;
    String timeStr;
    if (seconds == null) {
      return "00:00";
    }
    if (seconds <= 0)
      return "00:00";
    else {
      minute = (seconds / 60).floor();
      if (minute < 60) {
        second = seconds % 60;
        timeStr = unitFormat(minute) + ":" + unitFormat(second);
      } else {
        hour = (minute / 60).floor();
        if (hour > 99) return "99:59:59";
        minute = minute % 60;
        second = seconds - hour * 3600 - minute * 60;
        timeStr = unitFormat(hour) +
            ":" +
            unitFormat(minute) +
            ":" +
            unitFormat(second);
      }
    }
    return timeStr;
  }

  static String unitFormat(int i) {
    String retStr;
    if (i >= 0 && i < 10)
      retStr = "0" + i.toString();
    else
      retStr = "" + i.toString();
    return retStr;
  }

  static int getAge(DateTime brt) {
    int age = 0;
    DateTime dateTime = DateTime.now();
    if (dateTime.isBefore(brt)) {
      //出生日期晚于当前时间，无法计算
      return age;
    }
    int yearNow = dateTime.year; //当前年份
    int monthNow = dateTime.month; //当前月份
    int dayOfMonthNow = dateTime.day; //当前日期

    int yearBirth = brt.year;
    int monthBirth = brt.month;
    int dayOfMonthBirth = brt.day;
    age = yearNow - yearBirth; //计算整岁数
    if (monthNow <= monthBirth) {
      if (monthNow == monthBirth) {
        if (dayOfMonthNow < dayOfMonthBirth) age--; //当前日期在生日之前，年龄减一
      } else {
        age--; //当前月份在生日之前，年龄减一
      }
    }
    return age;
  }

  static String getTiemDate(int time, {List<String>? formats}) {
    if (formats == null) {
      formats = [yyyy, '-', mm, '-', dd];
    }
    return formatDate(DateTime.fromMillisecondsSinceEpoch(time), formats);
  }

  static String getWeekDay(DateTime date) {
    String week = '';
    switch (date.weekday) {
      case 1:
        week = '星期一';
        break;
      case 2:
        week = '星期二';
        break;
      case 3:
        week = '星期三';
        break;
      case 4:
        week = '星期四';
        break;
      case 5:
        week = '星期五';
        break;
      case 6:
        week = '星期六';
        break;
      case 7:
        week = '星期天';
        break;
    }
    return week;
  }

  ///获取相隔天数
  static int getBeApartDay(int startTime, int endTime) {
    print(endTime);
    DateTime startTimeCal = DateTime.parse(formatDate(
        DateTime.fromMillisecondsSinceEpoch(startTime ?? 0),
        [yyyy, '-', mm, '-', dd]));
    DateTime endTimeCal = DateTime.parse(formatDate(
        DateTime.fromMillisecondsSinceEpoch(endTime ?? 0),
        [yyyy, '-', mm, '-', dd]));
    print(startTimeCal);
    print(endTimeCal);
    return endTimeCal.difference(startTimeCal).inDays;
  }
  ///检测时间距离当前是今天 昨天 前天还是某个日期 跨年显示 年-月-日 不跨年显示 月-日
  static String getDateScope({dynamic checkDate}) {
    String temp = DateTime.now().toString();
    List listTemp = temp.split(" ");
    temp = listTemp[0];
    DateTime nowTime = DateTime.parse(temp);
    DateTime checkTime = _changeTimeDate(checkDate);

    print('checkTime=='+checkTime.toString());
    Duration diff = checkTime.difference(nowTime);
    print('diff====' + diff.toString());
    print('Duration====' +Duration(seconds: 60).toString());

    ///如果不同年 返回 年-月-日 小时:分钟 不显示秒及其.000
    if (checkTime.year != nowTime.year) {
      return checkTime.toString().substring(0, checkTime.toString().length - 7);
    }
    if ((diff < const Duration(hours: 24)) &&
        (diff > const Duration(hours: 0))) {
      return "今天 ${_dataNum(checkTime.hour)}:${_dataNum(checkTime.minute)}";
    } else if ((diff < const Duration(hours: 0)) &&
        (diff > const Duration(hours: -24))) {
      return "昨天 ${_dataNum(checkTime.hour)}:${_dataNum(checkTime.minute)}";
    } else if (diff < const Duration(hours: -24) &&
        diff > const Duration(hours: -48)) {
      return "前天 ${_dataNum(checkTime.hour)}:${_dataNum(checkTime.minute)}";
    }

    ///如果剩下都不是就返回 月-日 然后时间
    return checkTime.toString().substring(5, checkTime.toString().length - 7);
  }

  static String _dataNum(numb) {
    if (numb < 10) {
      return "0$numb";
    }
    return numb.toString();
  }

  static DateTime _changeTimeDate(time) {
    ///如果传进来的是字符串 13/16位 而且不包含-
    DateTime dateTime = DateTime.now();
    if (time is String) {
      if ((time.length == 13 || time.length == 16) && !time.contains("-")) {
        dateTime = timestampToDate(int.parse(time));
      } else {
        dateTime = DateTime.parse(time);
      }
    } else if (time is int) {
      dateTime = timestampToDate(time);
    }
    return dateTime;
  }

  static DateTime timestampToDate(int timestamp) {
    DateTime dateTime = DateTime.now();

    ///如果是十三位时间戳返回这个
    if (timestamp.toString().length == 13) {
      dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp);
    } else if (timestamp.toString().length == 16) {
      ///如果是十六位时间戳
      dateTime = DateTime.fromMicrosecondsSinceEpoch(timestamp);
    }
    return dateTime;
  }

}
